<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>javascript的数组方法</title>
    </head>
    <body></body>
    <script>
        /**
         *  首先我们先列举一下都有哪些方法：
         *      concat  every   filter  forEach     join    indexOf     lastIndexOf     map
         *      slice   reverse     some    sort    toString    valueOf
         *
         *
         * */

        //  1.数组合并 concat
        const zero = 0;
        const positiveNumbers = [1, 2, 3];
        const negativeNumbers = [-3, -2, -1];
        let numbers = negativeNumbers.concat(zero, positiveNumbers); // 结果 [-3,-2,-1,0,1,2,3]

        // 迭代器函数
        // 我们这里就使用判断是不是偶数来举例子
        function isEven(x) {
            return x % 2 === 0 ? true : false;
        }
        let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
        // every
        numbers.every(isEven);
        // some
        numbers.some(isEven);
        // forEach
        numbers.forEach((x) => console.log(isEven(x)));
        // map 或者 filter
        const myMap = numbers.map(isEven);
        const evenNumbers = numbers.filter(isEven);
        // reduce
        // 这个函数我们详细介绍下：一共有四个参数：previousValue、currentValue、index和array 后两个是可选参数
        numbers.reduce((previous, current) => previous + current); // 120
    </script>
</html>
